package com.pasu.breakreminder.activities;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.pasu.breakreminder.R;
import com.pasu.breakreminder.utils.DialogFactory;
import com.pasu.breakreminder.utils.Logger;
import com.pasu.breakreminder.utils.PreferencesHelper;

public class SettingsActivity extends Activity {
    private static final String TAG = "SettingsActivity";
    private static final String[] DAYS = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
    private static final boolean[] daysSelected = { false, false, false, false, false, false, false };
    private PreferencesHelper ph;
    private EditText mEditTime;
    private RadioButton mRadionTimeMin;
    private RadioButton mRadionTimeHour;
    private EditText mEditMessage;
    private CheckBox mCheckFullScreenNotification;
    private CheckBox mCheckEnableNotifications;
    private CheckBox mCheckEnableSound;
    private CheckBox mCheckEnableVibration;
    private CheckBox mCheckEnableLED;
    private CheckBox mCheckEnableAutoSchedule;
    private Button mSpinnerDays;
    private EditText mEditAutoFrom;
    private EditText mEditAutoTo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_view);
        ph = new PreferencesHelper(this);
        prepareUIElements();
    }

    private void prepareUIElements() {
        mEditTime = (EditText) findViewById(R.id.settings_edit_time);
        mRadionTimeMin = (RadioButton) findViewById(R.id.settings_radio_min);
        mRadionTimeHour = (RadioButton) findViewById(R.id.settings_radio_hour);
        mEditMessage = (EditText) findViewById(R.id.settings_edit_message);
        mCheckFullScreenNotification = (CheckBox) findViewById(R.id.settings_check_full_screen);
        mCheckEnableNotifications = (CheckBox) findViewById(R.id.settings_check_notifications);
        mCheckEnableSound = (CheckBox) findViewById(R.id.settings_check_sound);
        mCheckEnableVibration = (CheckBox) findViewById(R.id.settings_check_vibra);
        mCheckEnableLED = (CheckBox) findViewById(R.id.settings_check_led);
        mCheckEnableAutoSchedule = (CheckBox) findViewById(R.id.settings_check_auto_schedule);
        mSpinnerDays = (Button) findViewById(R.id.settings_spinner_days);
        mEditAutoFrom = (EditText) findViewById(R.id.settings_edit_auto_from);
        mEditAutoTo = (EditText) findViewById(R.id.settings_edit_auto_to);
        autoloadAllSettings();
        mCheckEnableAutoSchedule.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(SettingsActivity.this, "Not implemented yet", Toast.LENGTH_SHORT).show();
                buttonView.setChecked(false);
            }
        });
        mSpinnerDays.setEnabled(false);
        mEditAutoFrom.setEnabled(false);
        mEditAutoTo.setEnabled(false);
    }

    private void autoloadAllSettings() {
        Logger.i(TAG, "Loading settings");
        // general
        int sTime = ph.getInt(PreferencesHelper.Settings.General.TIME);
        if (sTime > 0) {
            mEditTime.setText(String.valueOf(sTime));
        }
        boolean sIsTimeInMin = ph.getBoolean(PreferencesHelper.Settings.General.TIME_IN_MIN);
        if (sIsTimeInMin) {
            mRadionTimeMin.setSelected(true);
            mRadionTimeHour.setSelected(false);
        } else {
            mRadionTimeMin.setSelected(false);
            mRadionTimeHour.setSelected(true);
        }
        String sMessage = ph.getString(PreferencesHelper.Settings.General.MESSAGE);
        if (sMessage != null && sMessage.length() > 0) {
            mEditMessage.setText(sMessage);
        }
        // notifications
        boolean sFullScreenNotif = ph.getBoolean(PreferencesHelper.Settings.Notification.ENABLE_FULL_SCREEN_NOTIFICATION);
        if (sFullScreenNotif) {
            mCheckFullScreenNotification.setChecked(true);
        }
        boolean sNotif = ph.getBoolean(PreferencesHelper.Settings.Notification.ENABLE_NOTIFICATIONS);
        if (sNotif) {
            mCheckEnableNotifications.setChecked(true);
        }
        boolean sSound = ph.getBoolean(PreferencesHelper.Settings.Notification.ENABLE_SOUND);
        if (sSound) {
            mCheckEnableSound.setChecked(true);
        }
        boolean sVibra = ph.getBoolean(PreferencesHelper.Settings.Notification.ENABLE_VIBRATE);
        if (sVibra) {
            mCheckEnableVibration.setChecked(true);
        }
        boolean sLed = ph.getBoolean(PreferencesHelper.Settings.Notification.ENABLE_LED);
        if (sLed) {
            mCheckEnableLED.setChecked(true);
        }
        // others
        boolean sAutoSchedule = ph.getBoolean(PreferencesHelper.Settings.Other.ENABLE_AUTO_SCHEDULE);
    }

    private void autosaveAllSettings() {
        Logger.i(TAG, "Saving settings");
        // general
        int sTime = Integer.parseInt(mEditTime.getText().toString());
        ph.save(PreferencesHelper.Settings.General.TIME, sTime);
        boolean sIsTimeInMin = mRadionTimeMin.isSelected() ? true : false;
        ph.save(PreferencesHelper.Settings.General.TIME_IN_MIN, sIsTimeInMin);
        String sMessage = mEditMessage.getText().toString();
        if (sMessage.length() > 0) {
            ph.save(PreferencesHelper.Settings.General.MESSAGE, sMessage);
        }
        // notifications
        ph.save(PreferencesHelper.Settings.Notification.ENABLE_FULL_SCREEN_NOTIFICATION, mCheckFullScreenNotification.isChecked());
        ph.save(PreferencesHelper.Settings.Notification.ENABLE_NOTIFICATIONS, mCheckEnableNotifications.isChecked());
        ph.save(PreferencesHelper.Settings.Notification.ENABLE_SOUND, mCheckEnableSound.isChecked());
        ph.save(PreferencesHelper.Settings.Notification.ENABLE_VIBRATE, mCheckEnableVibration.isChecked());
        ph.save(PreferencesHelper.Settings.Notification.ENABLE_LED, mCheckEnableLED.isChecked());
        // others
        // ph.save(PreferencesHelper.Settings.Other.ENABLE_AUTO_SCHEDULE,);
    }

    @Override
    public void onBackPressed() {
        String message = getString(R.string.dialog_save_settings);
        boolean needsRestart = true;
        if (needsRestart) {
            message = getString(R.string.dialog_save_settings_must_restart);
        }
        DialogFactory.createYesNoBackDialog(this, message, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                autosaveAllSettings();
                SettingsActivity.super.onBackPressed();
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                SettingsActivity.super.onBackPressed();
            }
        }).show();
    }
}
